$(function () {
    var type = $("#hide_type").val();
    if (type == 0)
        get_province();
    else if (type == 1) {
        var pid = $("#hide_pid").val();
        var pname = $("#hide_pname").val();
        get_city(pid, pname);
    } else if (type == 2) {
        var cid = $("#hide_cid").val();
        var cname = $("#hide_cname").val();
        get_district(cid, cname);
    }
});

function get_province() {
    var url = "/except/systemcontroller/getprvince.do";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var provinceinfo = "";
                var datas = data.data;
                $(datas).each(function () {
                    provinceinfo += '<div style="height: 50px;line-height: 50px;" onclick="chose_provice(\'' + this.pid + '\',\'' + this.name + '\')">' +
                        '<span style="margin-left: 10px;font-size: 16px;">' + this.name + '</span>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />';
                });
                $("#area").empty().append(provinceinfo);
                remove_advertising()
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    });
}

function get_city(pid, pname) {
    var url = "/except/systemcontroller/getcity.do";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    if (isempty(pid))
        pid = "null";
    var jsonstr = "{token:" + token + ",pid:" + pid + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var provinceinfo = "";
                var datas = data.data;
                $(datas).each(function () {
                    provinceinfo += '<div style="height: 50px;line-height: 50px;" onclick="chose_city(\'' + this.cid + '\',\'' + this.name + '\');">' +
                        '<span style="margin-left: 10px;font-size: 16px;">' + this.name + '</span>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />';
                });
                $("#area").empty().append(provinceinfo);
                remove_advertising()
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    });
}

function get_district(cid, cname) {
    var pid = $("#hide_pid").val();
    var pname = $("#hide_pname").val();
    var url = "/except/systemcontroller/getdistrict.do";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    if (isempty(pid))
        pid = "null";
    var jsonstr = "{token:" + token + ",cid:" + cid + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var provinceinfo = "";
                var datas = data.data;
                $(datas).each(function () {
                    provinceinfo += '<div style="height: 50px;line-height: 50px;" onclick="chose_district(\'' + this.did + '\',\'' + this.name + '\')">' +
                        '<span style="margin-left: 10px;font-size: 16px;">' + this.name + '</span>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;" />';
                });
                $("#area").empty().append(provinceinfo);
                remove_advertising()
            } else if(code == 20013) {
                $("#area_info").submit();
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    });
}

function chose_provice(pid, name) {
    $("#hide_pid").val(pid);
    $("#hide_pname").val(name);
    $("#hide_type").val(1);
    $("#area_info").attr("action", "/except/files/user/chose_city.jsp").submit();
}

function chose_city(cid, name) {
    $("#hide_cid").val(cid);
    $("#hide_cname").val(name);
    $("#hide_type").val(2);
    $("#area_info").attr("action", "/except/files/user/chose_city.jsp").submit();
}

function chose_district(did, name) {
    $("#hide_did").val(did);
    $("#hide_dname").val(name);
    $("#area_info").attr("action", "/except/files/user/add_new_address.jsp").submit();
}

